<?php
/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
 * 
 * =================================================================
 *                 MAGENTO EDITION USAGE NOTICE
 * =================================================================
 * This package designed for Magento COMMUNITY edition
 * aheadWorks does not guarantee correct work of this extension
 * on any other Magento edition except Magento COMMUNITY edition.
 * aheadWorks does not provide extension support in case of
 * incorrect edition usage.
 * =================================================================
 *
 * @category   AW
 * @package    AW_Vidtest
 * @copyright  Copyright (c) 2010-2011 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
 */
class AW_Vidtest_Helper_Data extends Mage_Core_Helper_Abstract
{
    /**
     * Retrives Rating Status
     * (Use constants from AW_Vidtest_Model_System_Config_Source_Rating)
     * @return int
     */
    public function confRatingStatus()
    {
        return Mage::getStoreConfig('vidtest/general/rating_status');
    }

    /**
     * Compare param $version with magento version
     * @param String $version
     * @return boolean
     */
	public function checkVersion($version)
	{
		return version_compare(Mage::getVersion(), $version, '>=');
	}
    
    /**
     * Retrives Cameled method name part (like YoWhoAreYou)
     * @param string $key Data key (like yo_who_are_you)
     * @return string
     */
    public function getDataKey($key)
    {
		if ($key){
			$in = explode("_", $key);
			$out = array();
			foreach ($in as $el){
				$out[] = ucwords(strtolower($el));
			}
			return implode("", $out);						
		}
		return 'NullKey';
    }

    /**
     * Save value in customer session
     * @param string $key
     * @param mixed $value
     * @return AW_Vidtest_Helper_Data
     */
    public function setValue($key, $value)
    {
        $method = 'set'.$this->getDataKey($key);
        $session = Mage::getSingleton('customer/session', array('name'=>'frontend'))->start();
        $session->$method($value);
        return $this;
    }

    /**
     * Load value from customer session
     * @param string $key
     * @return mixed
     */
    public function getValue($key)
    {
        $method = 'get'.$this->getDataKey($key);
        $session = Mage::getSingleton('customer/session', array('name'=>'frontend'))->start();        
        return $session->$method();
    }

    /**
     * Output message to customer in known place
     * @param string $message Error message
     * @return AW_Vidtest_Helper_Data
     */
    public function addCustomerError($message)
    {
        Mage::getSingleton('core/session')->addError($message);
        return $this;
    }
    
    /**
     * Output message to customer in known place
     * @param string $message Success message
     * @return AW_Vidtest_Helper_Data
     */
    public function addCustomerSuccess($message)
    {
        Mage::getSingleton('core/session')->addSuccess($message);
        return $this;
    }

    /**
     * Retrives Can User Group upload file on servise
     * @return boolean
     */
    public function canUpload()
    {        
        # Getting allowed for upload
        $groups = Mage::getStoreConfig('vidtest/general/allow_uploads');
        $groups = explode(",", $groups);
        $session = Mage::getSingleton('customer/session');        
        if ($session->isLoggedIn()){
            $cGroup = $session->getCustomer()->getGroupId();
        } else {
            $cGroup = '0';
        }
        foreach ($groups as $group){
            if ( ($group == $cGroup) || (!$group && !$cGroup) ) return true;
        }
        return false;
    }

    /**
     * Register rate in session for anticheat
     * @param int|string $videoId
     * @return AW_Vidtest_Helper_Data
     */
    public function registerRate($videoId)
    {
        $session = Mage::getSingleton('customer/session', array('name'=>'frontend'))->start();
        $rates = $session->getVidtestRates();
        if ( count($rates) ){
            $rates[] = $videoId;
        }else{
            $rates = array($videoId);
        }
        $session->setVidtestRates($rates);

        return $this;
    }

    /**
     * Retrives (rate registered) flag
     * @param int|string $videoId
     * @return boolean
     */
    public function isRateRegistered($videoId)
    {
        $session = Mage::getSingleton('customer/session', array('name'=>'frontend'))->start();
        $rates = $session->getVidtestRates();
        if ( isset($rates) ){
            return in_array($videoId, $rates);
        }else{
            return false;
        }
    }
}